package cn.yitun.chat.yitunserverimapicore.business.contact.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.contact.ContactCategoryDto;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.contact.service.ContactCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 
 * Date 2019-01-20 14:20:24<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Component
@ActionMapping(value = ActionTypeConstant.CONTACT_CATEGORY_ACTION_PUSH)
public class ContactCategoryAction {

	@Resource
	private ContactCategoryService contactCategoryService;

	/**
	 * 计数 由于已经设定
	 * @param socketSession
	 * @return
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage getCount(SocketSession socketSession) {
		Long ownerUserId = socketSession.getKey();
		return new ResultMessage(R.param("count", contactCategoryService.count(R.param("userId", ownerUserId))));
	}

	/**
	 * Date 2019-01-20 14:28:43<br>
	 * Description 获取联系人分组列表
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0002")
	public ResultMessage getList(SocketSession socketSession, @JsonDefine("data") ContactCategoryDto query) {

		Long userId = socketSession.getKey();
		query.setUserId(userId);
		PageHelper.startPage(query.getCurrentPageNo(), query.getPageSize());

		return new ResultMessage(R.param("page", new PageInfo<>(contactCategoryService.listVO(YitunMapUtil.java2Map(query, false)))));
	}

	/**
	 * Description 获取详情 <br>
	 * Date 2019-05-08 17:17:39<br>
	 * @param socketSession
	 * @param query
	 * @return ResultMessage
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0003")
	public ResultMessage getCategory(SocketSession socketSession, @JsonDefine("data") ContactCategoryDto query) {
		Long userId = socketSession.getKey();

		return new ResultMessage(R.param("category", contactCategoryService.getById(userId, query.getId())));
	}

	/**
	 * Description 新增分组 <br>
	 * Date 2019-05-08 17:18:00<br>
	 * @param socketSession
	 * @param contactCategory
	 * @return ResultMessage
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0004")
	public ResultMessage addCategory(SocketSession socketSession, @JsonDefine("data") ContactCategoryDto contactCategory) {
		if (NumUtil.isInValid(contactCategory.getUserId()))
			contactCategory.setUserId(socketSession.getKey());

		return contactCategoryService.add(contactCategory);
	}

	/**
	 * 
	 * Description 修改分组名 <br>
	 * Date 2019-05-08 17:18:20<br>
	 * 
	 * @param socketSession
	 * @param query
	 * @return ResultMessage
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0006")
	public ResultMessage updateName(SocketSession socketSession, @JsonDefine("data") ContactCategoryDto query) {
		query.setUserId(socketSession.getKey());
		if (NumUtil.isInValid(query.getId()) || YitunStringUtil.isEmpty(query.getName()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return contactCategoryService.updateName(query);
	}

	/**
	 * 
	 * Description 修改排序 <br>
	 * Date 2019-05-08 17:18:38<br>
	 * 
	 * @param socketSession
	 * @param query
	 * @return ResultMessage
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0007")
	public ResultMessage updateRank(SocketSession socketSession, @JsonDefine("data") ContactCategoryDto query) {
		if (NumUtil.isInValid(query.getId()) || query.getRank() == null)
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		query.setUserId(socketSession.getKey());
		return contactCategoryService.updateRank(query);
	}

	/**
	 * 
	 * Description 删除分组 <br>
	 * Date 2019-05-08 17:18:57<br>
	 * 
	 * @param socketSession
	 * @param query
	 * @return ResultMessage
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0008")
	public ResultMessage delete(SocketSession socketSession, @JsonDefine("data") ContactCategoryDto query) {
		query.setUserId(socketSession.getKey());
		return contactCategoryService.delete(query);
	}
}
